package com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkGenerateDialog

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.TempletSelectedTypeListInfor
import kotlinx.android.synthetic.main.aibk_adapter_item_attached_rank.view.*

class AibkTempletAttachedAdapter(
        val activity: AppCompatActivity,
        val templetSelectedTypeListInfor: TempletSelectedTypeListInfor,
        val indexA: Int,
        var indexB: Int
) : KBaseRecyclerAdapter() {

    override fun getHolderView(parent: ViewGroup?, position: Int): View? {
        return LayoutInflater.from(activity)
                .inflate(R.layout.aibk_adapter_item_attached_rank, parent, false)
    }

    override fun onBindViewHolder(holder: KBaseRecyclerViewHolder, position: Int) {

        val attachedCheckedPosition = templetSelectedTypeListInfor[indexA].CountList[indexB].AttachedCheckedPosition
        if (position == 0) {
            holder.convertView.BtnAttached.text = "无"
        } else {
            val featureName = templetSelectedTypeListInfor[indexA].CountList[indexB].FeatureList[position].FeatureName
            holder.convertView.BtnAttached.text = featureName
        }
        holder.convertView.BtnAttached.isSelected = position == attachedCheckedPosition
        holder.convertView.BtnAttached.setOnClickListener {

            if (templetSelectedTypeListInfor[indexA].CountList[indexB].FeatureList != null) {
                val featureListSize = templetSelectedTypeListInfor[indexA].CountList[indexB].FeatureList.size
                val attachedItemSize = featureListSize + 1
                for (i in 0 until attachedItemSize) {
                    if (i == position) {
                        templetSelectedTypeListInfor[indexA].CountList[indexB].AttachedCheckedPosition = i
                    }
                }
            }

            val attachedCheckedPosition = templetSelectedTypeListInfor[indexA].CountList[indexB].AttachedCheckedPosition
            if (attachedCheckedPosition > 0) {
                val featureName = templetSelectedTypeListInfor[indexA].CountList[indexB].FeatureList[attachedCheckedPosition-1].FeatureName
                clickAttachedListener!!.onClickAttached(featureName)
            }
            notifyDataSetChanged()
        }

    }

    override fun getItemCount(): Int {
        var itemSize = 1
        if (templetSelectedTypeListInfor[indexA].CountList[indexB].FeatureList != null) {
            val featureListSize = templetSelectedTypeListInfor[indexA].CountList[indexB].FeatureList.size
            itemSize += featureListSize
        }
        return itemSize
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    fun refreshIndexB(pos: Int) {
        indexB = pos
        notifyDataSetChanged()
    }

    private var clickAttachedListener: OnClickAttachedListener? = null

    /*
   * 设置点击事件
   * */
    interface OnClickAttachedListener {
        fun onClickAttached(specialFeatureCodes: String)
    }

    fun setClickAttachedListener(clickAttachedListener: OnClickAttachedListener?) {
        this.clickAttachedListener = clickAttachedListener
    }

}